package city.spring.modules.reception.controller;

import city.spring.modules.reception.entity.OldmanActiveEntity;
import city.spring.modules.reception.service.OldmanActiveService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：活动记录
 *
 * @author pxm
 * @date 2020-06-30 15:17:02
 */
@RestController
@RequestMapping("/oldman_active")
public class OldmanActiveController extends ApiController {
    private final OldmanActiveService oldmanActiveService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<OldmanActiveEntity, ?>> entityOrderFields;

    public OldmanActiveController(OldmanActiveService oldmanActiveService) {
        this.oldmanActiveService = oldmanActiveService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(OldmanActiveEntity::getId);
        entityOrderFields.add(OldmanActiveEntity::getTitle);
        entityOrderFields.add(OldmanActiveEntity::getTheme);
        entityOrderFields.add(OldmanActiveEntity::getAddress);
        entityOrderFields.add(OldmanActiveEntity::getType);
        entityOrderFields.add(OldmanActiveEntity::getOrganizer);
        entityOrderFields.add(OldmanActiveEntity::getPhone);
        entityOrderFields.add(OldmanActiveEntity::getStartTime);
        entityOrderFields.add(OldmanActiveEntity::getEndTime);
        entityOrderFields.add(OldmanActiveEntity::getImages);
        entityOrderFields.add(OldmanActiveEntity::getAttachments);
        entityOrderFields.add(OldmanActiveEntity::getGmtCreate);
        entityOrderFields.add(OldmanActiveEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>活动记录</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, OldmanActiveEntity entity) {
        LambdaQueryChainWrapper<OldmanActiveEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>活动记录</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, OldmanActiveEntity entity) {
        LambdaQueryChainWrapper<OldmanActiveEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<OldmanActiveEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>活动记录</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(oldmanActiveService.getActiveRecord(id));
    }

    /**
     * 添加一个 <strong>活动记录</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody OldmanActiveEntity entity) {
        oldmanActiveService.saveOldmanActive(entity);
        return success(entity);
    }

    /**
     * 修改一个 <strong>活动记录</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody OldmanActiveEntity entity) {
        entity.setId(id);
        oldmanActiveService.updateOldmanActive(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>活动记录</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        oldmanActiveService.deleteOldmanActive(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>活动记录</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        oldmanActiveService.deleteOldmanActive(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<OldmanActiveEntity> buildLambdaQuery(OldmanActiveEntity entity) {
        LambdaQueryChainWrapper<OldmanActiveEntity> lambdaQuery = oldmanActiveService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), OldmanActiveEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getTitle()), OldmanActiveEntity::getTitle, entity.getTitle());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getTheme()), OldmanActiveEntity::getTheme, entity.getTheme());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getAddress()), OldmanActiveEntity::getAddress, entity.getAddress());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getType()), OldmanActiveEntity::getType, entity.getType());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOrganizer()), OldmanActiveEntity::getOrganizer, entity.getOrganizer());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPhone()), OldmanActiveEntity::getPhone, entity.getPhone());
        lambdaQuery.eq(entity.getStartTime() != null, OldmanActiveEntity::getStartTime, entity.getStartTime());
        lambdaQuery.eq(entity.getEndTime() != null, OldmanActiveEntity::getEndTime, entity.getEndTime());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getImages()), OldmanActiveEntity::getImages, entity.getImages());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getAttachments()), OldmanActiveEntity::getAttachments, entity.getAttachments());
        lambdaQuery.eq(entity.getGmtCreate() != null, OldmanActiveEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, OldmanActiveEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<OldmanActiveEntity> buildQuery(OldmanActiveEntity entity) {
        QueryWrapper<OldmanActiveEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getTitle()), "title", entity.getTitle());
        wrapper.like(StringUtils.isNotBlank(entity.getTheme()), "theme", entity.getTheme());
        wrapper.like(StringUtils.isNotBlank(entity.getAddress()), "address", entity.getAddress());
        wrapper.like(StringUtils.isNotBlank(entity.getType()), "type", entity.getType());
        wrapper.like(StringUtils.isNotBlank(entity.getOrganizer()), "organizer", entity.getOrganizer());
        wrapper.like(StringUtils.isNotBlank(entity.getPhone()), "phone", entity.getPhone());
        wrapper.eq(entity.getStartTime() != null, "start_time", entity.getStartTime());
        wrapper.eq(entity.getEndTime() != null, "end_time", entity.getEndTime());
        wrapper.like(StringUtils.isNotBlank(entity.getImages()), "images", entity.getImages());
        wrapper.like(StringUtils.isNotBlank(entity.getAttachments()), "attachments", entity.getAttachments());
        wrapper.eq(entity.getGmtCreate() != null, "gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "gmt_modified", entity.getGmtModified());
        return wrapper;
    }

}
